<?php
/**
 * 文本投票
 * 模块定义
 * @author 回忆QQ125879930
 * @url
 */
defined('IN_IA') or exit('Access Denied');

//require("huiyi.support.php");

class Huiyi_textvoteModule extends WeModule {

    public $table_reply='huiyi_textvote_reply';
    public $table_setting='huiyi_textvote_setting';
    public $table_option='huiyi_textvote_option';
    public $table_log='huiyi_textvote_log';

	public function fieldsFormDisplay($rid = 0) {
        global $_W;
        $weid = $_W['weid'];
        $votes = pdo_fetchall("SELECT id, title FROM ".tablename($this->table_setting)." WHERE weid = :weid ORDER BY `id` DESC", array(':weid' => $weid));
        if (!empty($rid)) {
            $reply = pdo_fetch("SELECT * FROM ".tablename($this->table_reply)." WHERE rid = :rid ORDER BY `id` DESC", array(':rid' => $rid));
        }
        include $this->template('form');
	}

	public function fieldsFormValidate($rid = 0) {
		return '';
	}

	public function fieldsFormSubmit($rid) {
		global $_GPC, $_W;
        $id = intval($_GPC['reply_id']);

        $data = array(
            'rid' => $rid,
            'state' => $_GPC['state'],
            'create_time' => TIMESTAMP,
            'vid' => intval($_GPC['vid']),
            'expire' => intval($_GPC['expire']),
            'exit_key' => $_GPC['exit_key'],
        );

        if (empty($id)) {
            pdo_insert($this->table_reply, $data);
        } else {
            pdo_update($this->table_reply, $data, array('id' => $id));
        }
	}

	public function ruleDeleted($rid) {
		//删除规则时调用，这里 $rid 为对应的规则编号
		global $_W;
        //删除回复
//		$replies = pdo_fetchall("SELECT id FROM ".tablename($this->table_vote)." WHERE rid = '$rid'");
//		$deleteid = array();
//		if (!empty($replies)) {
//			foreach ($replies as $index => $row) {
//				$deleteid[] = $row['id'];
//			}
//		}
//		pdo_delete($this->table_vote, "id IN ('".implode("','", $deleteid)."')");
		return true;
	}

	public function settingsDisplay($settings) {
		//点击模块设置时将调用此方法呈现模块设置页面，$settings 为模块设置参数, 结构为数组。这个参数系统针对不同公众账号独立保存。
		//在此呈现页面中自行处理post请求并保存设置参数（通过使用$this->saveSettings()来实现）
	}

}